package com.atom.app.lifecycle.bean

import androidx.room.*
import com.atom.app.lifecycle.db.AbstractUpdateable
import java.util.*

interface Record {

    @Entity(tableName = "record_table")
    open class Bean : AbstractUpdateable {

        @PrimaryKey(autoGenerate = true)
        @ColumnInfo(name = "id")
        var id: Long? = null

        @ColumnInfo(name = "record")
        var record: Long

        @ColumnInfo(name = "title")
        var title: String? = null

        @ColumnInfo(name = "describe")
        var describe: String? = null

        @ColumnInfo(name = "path")
        var path: String? = null

        // 必须的
        constructor() : super()

        @Ignore
        constructor(title: String?, describe: String?, path: String?) : super() {
            this.title = title
            this.describe = describe
            this.path = path
        }

        init {
            val instance = Calendar.getInstance()
            this.record = Date(
                instance.get(Calendar.YEAR),
                instance.get(Calendar.MONTH) + 1,
                instance.get(Calendar.DAY_OF_MONTH)
            ).time
        }

        override fun toString(): String {
            return "${super.toString()} id=$id , name=$title "
        }

    }

    @Dao
    interface DAO {

        @Insert(onConflict = OnConflictStrategy.IGNORE)
        suspend fun insert(account: Bean): Long

        @Query("DELETE FROM record_table")
        fun deleteAll(): Int

        @Query("Select * from record_table")
        suspend fun selectAll(): List<Bean>

        @Query("Select * from record_table where record_table.id = :id")
        suspend fun selectSingleById(id: Long): Bean?

        @Query("Select * from record_table where record_table.uuid = :uuid")
        suspend fun selectSingleByUUID(uuid: String): Bean?

        @Query("Select * from record_table where record_table.title = :title")
        suspend fun selectSingleByTitle(title: String): Bean?

    }

}